

clear


%% data (annual)
data0=readtable('../../../Data/data_main_sample.csv');


%% 5-year panel
data=data0(arrayfun(@(n)isempty(setdiff(data0.year(n),1875:5:2010)),1:size(data0,1)),1:8);
ccodes=unique(data.ccode);
for n=1:length(ccodes)
    dn=data(data.ccode==ccodes(n),:);
    for t=1875:5:2010
        if ~isempty(dn.year==t)
            dn0=data0(data0.ccode==ccodes(n)&data0.year>=t-5&data0.year<=t,:);
            if size(dn0,1)==6
                dn.y(dn.year==t)=log(dn0.Y(end))-log(dn0.Y(1));
                dn.I(dn.year==t)=sum(dn0.I(2:end))==5;
            else
                dn.y(dn.year==t)=NaN;
                dn.I(dn.year==t)=NaN;
            end
        end
    end
    data(data.ccode==ccodes(n),:)=dn;
end
    

%%
clearvars -except data
save('data_5y.mat')


